#!/usr/bin/env python3

from launch import LaunchDescription
from launch_ros.actions import ComposableNodeContainer, Node
from launch_ros.descriptions import ComposableNode
from launch.actions import DeclareLaunchArgument, TimerAction
from launch.substitutions import LaunchConfiguration
from launch.conditions import IfCondition

def generate_launch_description():
    # 声明启动参数
    mode_arg = DeclareLaunchArgument(
        'mode',
        default_value='component',
        description='Launch mode: component or traditional'
    )
    
    # 组件模式 - 单进程多组件
    component_container = ComposableNodeContainer(
        name='performance_component_container',
        namespace='',
        package='rclcpp_components',
        executable='component_container',
        composable_node_descriptions=[
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentPublisher',
                name='comp_pub_1',
                parameters=[{'publish_frequency': 10.0, 'message_prefix': 'Comp1'}]
            ),
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentPublisher',
                name='comp_pub_2',
                parameters=[{'publish_frequency': 10.0, 'message_prefix': 'Comp2'}]
            ),
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentPublisher',
                name='comp_pub_3',
                parameters=[{'publish_frequency': 10.0, 'message_prefix': 'Comp3'}]
            ),
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentSubscriber',
                name='comp_sub_1'
            ),
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentSubscriber',
                name='comp_sub_2'
            ),
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentSubscriber',
                name='comp_sub_3'
            )
        ],
        output='screen',
        condition=IfCondition(LaunchConfiguration('mode'))
    )
    
    # 传统模式 - 多进程
    traditional_nodes = [
        Node(
            package='multi_launch_demo',
            executable='publisher_node',
            name='trad_pub_1',
            parameters=[{'publish_frequency': 10.0, 'message_prefix': 'Trad1'}],
            output='screen'
        ),
        Node(
            package='multi_launch_demo',
            executable='publisher_node',
            name='trad_pub_2',
            parameters=[{'publish_frequency': 10.0, 'message_prefix': 'Trad2'}],
            output='screen'
        ),
        Node(
            package='multi_launch_demo',
            executable='publisher_node',
            name='trad_pub_3',
            parameters=[{'publish_frequency': 10.0, 'message_prefix': 'Trad3'}],
            output='screen'
        ),
        Node(
            package='multi_launch_demo',
            executable='subscriber_node',
            name='trad_sub_1',
            output='screen'
        ),
        Node(
            package='multi_launch_demo',
            executable='subscriber_node',
            name='trad_sub_2',
            output='screen'
        ),
        Node(
            package='multi_launch_demo',
            executable='subscriber_node',
            name='trad_sub_3',
            output='screen'
        )
    ]
    
    return LaunchDescription([
        mode_arg,
        component_container,
        *traditional_nodes
    ])
